package br.com.leopard.erp.gym.converters.date;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;

import org.apache.commons.lang3.time.DateUtils;

@FacesConverter(value = "dateTimeConverter")
public class DateTimeConverter implements Converter {

	private static String PATTERN = "dd/MM/yyyy HH:mm:ss";

	@Override
	public Object getAsObject(FacesContext arg0, UIComponent arg1, String arg2) {
		try {
			return DateUtils.parseDate(arg2, PATTERN);
		} catch (ParseException e) {
			return null;
		}
	}

	@Override
	public String getAsString(FacesContext arg0, UIComponent arg1, Object arg2) {
		Date data = (Date) arg2;

		SimpleDateFormat format = new SimpleDateFormat(PATTERN);
		return format.format(data);
	}
}
